# -*- coding: utf-8 -*-

import allure
from dz_service.page_elements.el_risk import risk
from dz_service.page_elements.el_common import common
from dz_service.page_elements.el_home import home
from dz_service.function.fun_service_utils import ServiceUtils
from common.page import Page
from time import sleep


class Risk:
    @allure.step('step:业务办理前置检查')
    def check_risk(self):
        Page.click(home, '首页', '风险测评')
        ServiceUtils.assert_element([(risk, '检查元素', '风险等级引导', '您的风险承受能力等级为', '打开我的风险类型页失败。')],
                                    log='打开我的风险类型页成功。')
        # 检查我的风险类型
        Page.check_page_loading(10)
        Page.click(risk, '风险测评', '重新评估')
        # ServiceUtils.assert_element([(risk, '检查元素', '试题', '1、您的主要收入来源是：', '打开风险测试页失败。')],
        #                             log='打开风险测试页成功。')

    @allure.step('step:风险测评')
    def risk(self, answer_dict):
        # 开始答题
        sleep(1)
        for i in answer_dict:
            sleep(0.7)
            if answer_dict[i] == "A" or answer_dict[i] == "a":
                el = Page.el_xpath(f"//*[@for='answer{i}1']")
                Page.el_js_click(el)

            if answer_dict[i] == "B" or answer_dict[i] == "b":
                el = Page.el_xpath(f"//*[@for='answer{i}2']")
                Page.el_js_click(el)

            if answer_dict[i] == "C" or answer_dict[i] == "c":
                el = Page.el_xpath(f"//*[@for='answer{i}3']")
                Page.el_js_click(el)

            if answer_dict[i] == "D" or answer_dict[i] == "d":
                el = Page.el_xpath(f"//*[@for='answer{i}4']")
                Page.el_js_click(el)

            if answer_dict[i] == "E" or answer_dict[i] == "e":
                el = Page.el_xpath(f"//*[@for='answer{i}5']")
                Page.el_js_click(el)
        # 提交测评
        Page.click(risk, '风险测评', '确定')
        # 阅读并同意
        Page.click(common, '签署协议', '我已阅读并同意')
        Page.click(common, '公共页面', '确定')
        ServiceUtils.assert_element([(risk, '检查元素', '办理成功', '办理成功', '办理成功文本对比失败')],
                                    log='办理成功文本对比成功')
